import { DOCMAP, IP, PORT } from '../constants'
import { vector } from './source'
const gaodeMapLayer = new ol.layer.Tile({
  title: '高德地图',
  source: new ol.source.XYZ({
    url: 'http://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={x}&y={y}&z={z}',
    wrapX: false,
    crossOrigin: 'Anonymous',
  }),
})
const satMapLayer = new ol.layer.Tile({
  title: '卫星地图',
  source: new ol.source.XYZ({
    url: 'http://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=6&x={x}&y={y}&z={z}',
    wrapX: false,
    crossOrigin: 'Anonymous',
  }),
})

export const initMap = () => {
  const map = new ol.Map({
    target: 'map',
    layers: [satMapLayer, gaodeMapLayer],
    view: new ol.View({
      center: [114.37, 30.48668],
      zoom: 12,
      minZoom: 4,
      maxZoom: 19,
      projection: 'EPSG:4326',
    }),
  })
  const ggLayer = new Zondy.Map.Doc('', DOCMAP, {
    IP,
    PORT,
    crossOrigin: 'Anonymous',
  })
  // 地图控件
  // 1. 视图跳转控件
  const ZoomToExtent = new ol.control.ZoomToExtent({
    extent: [114.34, 30.47668, 114.41, 30.49668],
  })
  map.addControl(ZoomToExtent)
  map.addLayer(ggLayer)
  map.addLayer(vector)
  return map
}
export const selVect = (map) => {
  // console.log(map)
  const layers = map.getLayers().getArray()
  layers[1].setVisible(true)
}
export const selSat = (map) => {
  const layers = map.getLayers().getArray()
  layers[1].setVisible(false)
}
